package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.DestinationObject;
import cn.wolfcode.wolf2w.query.RegionObject;
import cn.wolfcode.wolf2w.repository.DestinationRepository;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import sun.security.krb5.internal.crypto.Des;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationRepository repository;
    @Autowired
    private RegionRepository regionRepository;

    @Autowired
    private MongoTemplate template;


    @Override
    public void update(Destination destination) {
        repository.save(destination);
    }

    @Override
    public void save(Destination destination) {
        repository.save(destination);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Destination get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<Destination> list() {
        return repository.findAll();
    }

    @Override
    public List<Destination> getDestByRegionId(String id) {
        List<String> refIds = regionRepository.findById(id).orElse(null).getRefIds();
        repository.findAllByIdIn(refIds);

        return repository.findAllByIdIn(refIds);

    }

    @Override
    public Page<Destination> query(DestinationObject qo) {
        Query query = new Query();
        if (StringUtils.hasLength(qo.getKeyword())) {
            query.addCriteria(Criteria.where("name").is(qo.getKeyword()));
        }
        if (StringUtils.hasLength(qo.getParentId())) {
            query.addCriteria(Criteria.where("parentId").is(qo.getParentId()));
        } else {
            query.addCriteria(Criteria.where("parentId").is(null));
        }
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1,
                qo.getPageSize(), Sort.Direction.ASC, "_id");

        return DBHelper.query(template, query, pageable, Destination.class, qo);
    }

    @Override
    public void saveOrUpdate(Destination destination) {
        if (StringUtils.hasLength(destination.getId())) {
            repository.save(destination);
        } else {
            destination.setId(null);
            template.insert(destination);
        }
    }

    @Override
    public List<Destination> queryToasts(String parentId) {
        if (!StringUtils.hasLength(parentId)) {
            return new ArrayList<>();
        }
        List<Destination> list = new ArrayList<>();
        Destination des = this.get(parentId);
        list.add(des);
//        while (true) {
//            if (StringUtils.hasLength(des.getParentId())) {
//                des = this.get(des.getParentId());
//                list.add(des);
//            } else {
//                Collections.reverse(list);
//                return list;
//            }
//        }
        while (StringUtils.hasLength(des.getParentId())) {
            des = this.get(des.getParentId());
            list.add(des);
        }
        Collections.reverse(list);
        return list;
    }

    @Override
    public List<Destination> getDestByRegionIdForapi(String regionId) {
        List<Destination> des = new ArrayList<>();
        if ("-1".equals(regionId)) {
            des = repository.findByParentName("中国");

        } else {
            des = this.getDestByRegionId(regionId);
        }
        for (Destination destination : des) {
            Pageable pageable = PageRequest.of(0, 7);
            List<Destination> list = repository.findByParentId(destination.getId(), pageable);
            destination.setChildren(list);
        }
        return des;
    }

    @Override
    public List<Destination> getDestByDeep(int deep) {
        List<Destination> destinations = repository.findByDeep(deep);

        return destinations;
    }

    @Override
    public void updateForinfo(String id, String info) {
        Destination destination = this.get(id);
        destination.setInfo(info);
        repository.save(destination);
    }

    @Override
    public String getByName(String keyword) {
        return repository.findByName(keyword).getId();

    }

    @Override
    public Destination queryName(String keyword) {
        return repository.findByName(keyword);
    }


}
